﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace Technewlogic.FileXChange
{
	public class FileServiceLogic
	{
		public byte[] Get(string filename)
		{
			byte[] buffer = new byte[0];
			using (FileStream fs = new FileStream(UploadPath + "\\" + filename,
				FileMode.Open, FileAccess.Read))
			{
				buffer = new byte[fs.Length];
				fs.Read(buffer, 0, (int)fs.Length);
				fs.Close();
			}

			return buffer;
		}

		public void Post(string filename, byte[] file)
		{
			if (string.IsNullOrEmpty(filename))
				throw new ArgumentException("Filename must be specified.");

			using (FileStream fs = new FileStream(UploadPath + "\\" + filename,
				FileMode.Create, FileAccess.Write, FileShare.None))
			{
				fs.Write(file, 0, file.Length);
				fs.Close();
			}
		}

		public void Delete(string filename)
		{
			File.Delete(UploadPath + "\\" + filename);
		}

		public string UploadPath
		{
			get
			{
				string path = Properties.Settings.Default.UploadPath;
				if (!Directory.Exists(path))
					Directory.CreateDirectory(path);

				path = new DirectoryInfo(path).FullName;
				return path;
			}
		}
	}
}
